package com.org.cys.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.org.cys.controller.base.BaseController;
import com.org.cys.pojo.system.data.PrivilegeEnum;
import com.org.cys.service.system.PrivilegeService;
import com.org.cys.util.ExportExcel;
import com.org.cys.util.MapUtil;
import com.org.cys.util.PageData;  
  
/**
 * 权限Controller
 * @author caiyushen
 * */  
@Controller  
@RequestMapping("/privilege")  
public class PrivilegeController extends BaseController{  
    
	private static Logger logger = Logger.getLogger(PrivilegeController.class);
	
	@Resource(name="privilegeServiceImpl")
    private PrivilegeService privilegeService;  
	
   
   /**
     * 去用户列表页面
     * @author caiyushen
     * */
    @RequestMapping(value="/toList",method = RequestMethod.GET)  
    public String toList() throws Exception{  
        return "privilege/list";  
    }  
	
	
    /**
     * 获取列表信息
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public PageData list(){
    	PageData result = new PageData();
    	try{
    		List<PageData> parentList = privilegeService.getParentList(new PageData());
    		PrivilegeEnum.convert(parentList);
    		int parentListSize = parentList.size();
    		for(int i=0;i<parentListSize;i++){
    			PageData parentData = parentList.get(i);
    			parentData.put("state", "closed");//默认父级树不展开
    			List<PageData> childrenList = privilegeService.getListByParentId(MapUtil.getLong(parentData, "id"));
    			PrivilegeEnum.convert(childrenList);
    			parentData.put("children", childrenList);
    		}
    		result.put("dataList", parentList);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    	}
    	return result;
    }
    
    
    
    
    /**
     * 新增Privilege
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public PageData add(@RequestBody PageData pd){
    	logger.info("method add() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		privilegeService.save(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
   
    
    /**
     * 修改Privilege
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public PageData update(@RequestBody PageData pd){
    	logger.info("method update() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		privilegeService.update(pd);
    		result.put("success", true);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
    
    /**
     * 删除Privilege
     * @author caiyushen
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE)
    public PageData delete(@RequestBody PageData pd){
    	logger.info("method delete() param: "+pd.toString());
    	PageData result = new PageData();
    	try{
    		//判断是否有子级数据
    		List<PageData> childrenList = privilegeService.getListByParentId(MapUtil.getLong(pd, "id"));
    		if(childrenList.size()>0){
        		result.put("success", false);
        		result.put("error", "请删除该权限下的子权限！");
    		}else{
    			privilegeService.delete(pd);
        		result.put("success", true);
    		}
    		
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    		result.put("success", false);
    	}
    	return result;
    }
    
    /**
     * 导出Excel数据
     * */
    @RequestMapping(value="/exportExcel",method = RequestMethod.GET)
    public ModelAndView exportExcel(){
    	ModelAndView mv = new ModelAndView();
    	PageData pd = new PageData();
    	pd = this.getPageData();
    	try{
    		Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
		    titles.add("权限名称"); //1	
		    titles.add("地址URL"); //2	
		    titles.add("权限类型：1：菜单   2：按钮"); //3	
		    titles.add("权限编码"); //4	
		    titles.add("备注"); //5	
			dataMap.put("titles", titles);
			List<PageData> varOList = privilegeService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("privilegeName"));//1
				vpd.put("var2", varOList.get(i).getString("privilegeUrl"));//2
				vpd.put("var3", varOList.get(i).getString("privilegeType"));//3
				vpd.put("var4", varOList.get(i).getString("privilegeCode"));//4
				vpd.put("var5", varOList.get(i).getString("privilegeRemark"));//5
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ExportExcel erv = new ExportExcel();
			mv = new ModelAndView(erv,dataMap);
    	}catch(Exception e){
			logger.error(e.toString(), e);
		}
    	
    	return mv;
    }
    
    /**
     * 获取所有父级Privilege数据
     * */
    @ResponseBody
    @RequestMapping(value="/getParentList",method = RequestMethod.GET)
    public PageData getParentList(){
    	PageData result = new PageData();
    	try{
    		List<PageData> dataList = privilegeService.getParentList(new PageData());
    		PageData pd = new PageData();
    		pd.put("id", null);
    		pd.put("privilegeName", "无");
    		dataList.add(pd);
    		result.put("dataList", dataList);
    	}catch(Exception e){
    		logger.error(e.toString(),e);
    	}
    	return result;
    }
    
}  